package com.nd.qa.shareplate.jdom;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

public class QueryXmlFileDemo {

	public static void main(String[] args) {
		 try {
	         File file = new File("E:\\baiduyun\\input.txt");
	         SAXBuilder saxBuilder = new SAXBuilder();
	         //读取文件
	         Document document = saxBuilder.build(file);
	         //获取根节点
	         Element classElement = document.getRootElement();
	         //获取子节点
	         List<Element> elements = classElement.getChildren();
	         //遍历根节点下的所有子节点
	         for (Element element : elements) {
				//获取子节点名称
	        	 System.out.println("\nCurrent Element :" 
	                     + element.getName());
	        	 //获取子节点中的属性
	        	 Attribute attribute = element.getAttribute("company");
	        	 //获取子节点中属性值
	        	 System.out.println("company : " 
	                     + attribute.getValue() );
	        	 //再获取该子节点下的子节点信息
	        	 List<Element> carNameList = element.getChildren();
	        	 for (Element carElement : carNameList) {
	        		 System.out.print("car name : ");
	                 System.out.println(carElement.getText());
	                 System.out.print("car type : ");
	                 Attribute typeAttribute = carElement.getAttribute("type");
	                 if(typeAttribute !=null)
	                    System.out.println(typeAttribute.getValue());
	                 else{
	                    System.out.println("");
	                 }
				}
			}
	      }catch(JDOMException e){
	         e.printStackTrace();
	      }catch(IOException ioe){
	         ioe.printStackTrace();
	      }
	}
}
